package org.skyui.views;

import org.skyui.beanmanagers.BeanRelationManager;
import org.skyui.beanmanagers.BeanSetManager;
import org.skyui.beans.AbstractBean;
import org.skyui.controllers.BeansPopupSelectTableController;
import org.skyui.jpa.JPAManager;

import com.vaadin.data.util.BeanItem;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.ui.Window;

public class BeansTablePopupSelectView extends AbstractBeansTableView {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1726451270426940562L;
	protected BeanItemContainer popupSelectParent;

	BeanRelationManager parentBeanManager;

	public BeansTablePopupSelectView(BeanSetManager beanManager,
			BeanItemContainer container, BeanRelationManager parentBeanManager) {
		super("", "", true, beanManager, false);
		this.setController(new BeansPopupSelectTableController(beanManager, this));
		this.popupSelectParent = container;
		this.parentBeanManager = parentBeanManager;
		this.getLayout().removeComponent(this.getHoriz());
	}

	public void itemClick(ItemClickEvent event) {

		BeanItem<? extends AbstractBean> item = (BeanItem<? extends AbstractBean>) event
				.getItem();

		this.popupSelectParent.addBean(item.getBean());
		AbstractBean b = item.getBean();
		JPAManager.addManyToManyRelation(
				this.parentBeanManager.getParentBean(), b);

		Window subWindow = getWindow();
		Window appWindow = (Window) subWindow.getParent();
		appWindow.removeWindow(subWindow);
		return;

	}

}
